package shop.ynubeggar.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import shop.ynubeggar.mapper.ProductMapper;
import shop.ynubeggar.model.dataobject.ProductDO;
import shop.ynubeggar.request.ConfirmOrderRequest;
import shop.ynubeggar.service.ProductService;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductDO> implements ProductService {

    @Override
    @DS("yeat_product")
    public List<ProductDO> lockStock(ConfirmOrderRequest confirmOrderRequest) {
        //        锁定商品库存
        Map<Long, Integer> productNums = confirmOrderRequest.getProductIdAndNums();
        Set<Long> idSet = productNums.keySet();
        List<ProductDO> productDOList = listByIds(idSet);
        for (ProductDO productDO : productDOList) {
            productDO.setLockStock(productNums.get(productDO.getId()) + productDO.getLockStock());
        }
        updateBatchById(productDOList);
        return productDOList;
    }
}
